#include "MyApplication.h"
#include "GullWindows.h"
#include<iostream>
#include "GUID.h"
#include "Vector3D.h"
#include "Matrix4x3.h"
#include "Timer.h"
#include "CameraNode.h"
#include "Scene.h"
#include "LightNode.h"
#include "MeshTestNode.h"
#include "LightProperties.h"
#include "PlanMesh.h"
#include "png.h"


using namespace gull::core;
using namespace std;

Timer t;
int frame = 0;


MyApplication::MyApplication()
{
	MainWindow mainWindow(640,480);
	CameraNode* cam = new CameraNode("camera");
	MeshTestNode* mesh = new MeshTestNode("triangle");
	LightNode* light = new LightNode("light");
	MeshTestNode* meshLight = new MeshTestNode("meshLight");
	PlanMesh* meshPlan = new PlanMesh("plan");
	Scene* scene = new Scene();
	
	LightProperties *lp = new LightProperties();;
	lp->setDiffuse(Vector3D(1.0,1.0,1.0));
	light->setProperties(*lp);
	
	
	TransformController* camTr = static_cast<TransformController*>(cam->getController(TRANSFORM));
	TransformController* meshTr = static_cast<TransformController*>(mesh->getController(TRANSFORM));
	TransformController* meshLightTr = static_cast<TransformController*>(meshLight->getController(TRANSFORM));
	TransformController* meshPlanTr = static_cast<TransformController*>(meshPlan->getController(TRANSFORM));
	
	camTr->setPosition(Vector3D(0.0,1.0,8.0));
	camTr->rotateAboutAxis(Vector3D(1.0,0.0,0.0), -20);
	
	meshTr->setPosition(Vector3D(0.0,0.0,0.0));
	
	meshLightTr->setPosition(Vector3D(0.0,1.0,1.0));
	meshLightTr->rotateAboutAxis(Vector3D(1.0,0.0,0.0), -45);
	
	meshPlanTr->setPosition(Vector3D(0.0,-1.0,5.0));
	
	
	scene->addNode(meshPlan);
	//meshLight->addChild(light);
	scene->addNode(meshLight);
	scene->addNode(mesh);
	scene->addNode(cam);
	scene->attachCamera(*cam);
	t.start();
	
	
}


void 
MyApplication::applicationOnLoad() const
{
	const Scene* scene = theGullSceneManager->getScene();
	SceneNode* root = scene->getRoot();
	CameraNode* cam = static_cast<CameraNode*>(root->getChild("camera"));
	MeshTestNode* mesh = static_cast<MeshTestNode*>(root->getChild("triangle"));
	//LightNode* light = static_cast<LightNode*>(root->getChild("light"));
	MeshTestNode* meshLight = static_cast<MeshTestNode*>(root->getChild("meshLight"));
	
	frame++;
	//Timer::wait(10);
	if (t.getTime() >= 1000)
	{
		cout << frame << endl;
		frame = 0;
		t.restart();
	}
	if(cam)
	{
		//cam->getController()->rotateAboutAxis(Vector3D(0,1,0), 0.1);
	}
	
	if(mesh)
	{
		//mesh->getController()->rotateAboutAxis(Vector3D(0,1,0), speed*2.0);
		
	}
	if(meshLight)
	{
		TransformController* meshLightTr = static_cast<TransformController*>(meshLight->getController(TRANSFORM));
		meshLightTr->getMatrix()->rotateY(0.08);	
	}
}

